package mvp.com.bbc.rcjavaretrofitdaggermvp.mvp.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;
import mvp.com.bbc.rcjavaretrofitdaggermvp.R;
import mvp.com.bbc.rcjavaretrofitdaggermvp.app.contact.ArtListContact;
import mvp.com.bbc.rcjavaretrofitdaggermvp.app.utils.ToastUtlis;
import mvp.com.bbc.rcjavaretrofitdaggermvp.baseAF.BaseFragment;
import mvp.com.bbc.rcjavaretrofitdaggermvp.bean.CommunityListBean;
import mvp.com.bbc.rcjavaretrofitdaggermvp.di.component.DaggerArtListComponent;
import mvp.com.bbc.rcjavaretrofitdaggermvp.di.modules.ArtListModules;
import mvp.com.bbc.rcjavaretrofitdaggermvp.mvp.adapter.HotMomAdapter;
import mvp.com.bbc.rcjavaretrofitdaggermvp.presenter.ArtListPresenter;
import tech.michaelx.loadinglibrary.LoadingLayout;

/**
 * Created by ${cbf} on 2017/12/2.
 */

public class HotMomFragment extends BaseFragment implements ArtListContact.ArtListView {
    @BindView(R.id.rv_base_recyclerview)
    RecyclerView RV;
    @BindView(R.id.smartrefresh)
    SmartRefreshLayout smartrefresh;
    @BindView(R.id.loadinglayout)
    LoadingLayout loadinglayout;
    private ArtListPresenter presenter;
    private String type;
    private int page = 1;
    private HotMomAdapter adapter;


    @Override
    protected int setContentLayout() {
        return R.layout.base_refresh_loading;
    }

    @Override
    protected void findViews() {

    }

    @Override
    protected void init() {
        Bundle bundle = getArguments();
        type = bundle.getString("type");
        if (type == null) {
            type = "1";
        }
        RV.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new HotMomAdapter(getContext(), type);
        RV.setAdapter(adapter);
        presenter = DaggerArtListComponent
                .builder()
                .artListModules(new ArtListModules(this))
                .build()
                .getPresenter();
        presenter.artListPresenter(type, page + "", "80");
    }

    @Override
    protected void setListeners() {
        smartrefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                presenter.artListPresenter(type, page + "", "80");
                refreshlayout.finishRefresh(500);
                loadinglayout.loadComplete();
            }
        });
        smartrefresh.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                presenter.artListPresenter(type, page + "", "80");
                refreshlayout.finishLoadmore(500);
                loadinglayout.loadComplete();
            }
        });
        loadinglayout.setOnRetryLoadListener(new LoadingLayout.OnRetryLoadListener() {
            @Override
            public void onReLoad() {
                presenter.artListPresenter(type, page + "", "80");
            }
        });

    }

    @Override
    public void onClick(View view) {

    }

    public static Fragment newInstance(String type) {
        HotMomFragment hotMomFragment = new HotMomFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        hotMomFragment.setArguments(bundle);
        return hotMomFragment;
    }

    @Override
    public void artListView(CommunityListBean listBean) {
        List<CommunityListBean.ArtBean> art = listBean.getArt();
        loadinglayout.showLoading();
        if (art == null && art.size() < 0) {
            loadinglayout.showEmpty();
        } else {
            if (page == 1) {
                adapter.setData(art);
            } else {
                adapter.addData(art);
            }
            loadinglayout.loadComplete();
        }
    }

    @Override
    public void onEvent(String action) {
        super.onEvent(action);
        if (action.equals("网络请求失败")) {
            loadinglayout.showLoading();
        }
        if (action.equals("网络请求成功")) {
            loadinglayout.loadComplete();
        }
    }
}
